<?php
class Transfer_record_model extends BZ_Model
{
    protected $_table  = 'transfer_record';
    protected $_table1 = 'withdraw';
    
    /**
     * 扫码用户提现记录
     */
    public function page_list($params, $start, $count)
    {
        $this->db->select('transfer_record.transfer_id, total_amount, total_num, success_num, success_amount, withdraw.transfer_status, transfer_record.created_at AS transfer_created_at, transfer_record.updated_at AS transfer_updated_at, withdraw.withdraw_id, withdraw.uid, amount, amount_carry, is_from, withdraw.state, bank_id, bank_card, cardholder, bank_name, mobile_phone, audit_status, start_time, pay_time, LEFT(transfer_record.created_at, 7) AS month ', FALSE);
        $this->db->from($this->_table);
        $this->db->join($this->_table1, 'transfer_record.transfer_id = withdraw.transfer_id', 'INNER');

        $this->db->where('is_from', 3); //扫码提现
        $this->db->where_in("withdraw.uid", $params['uid']); //提现用户

        if ( ! empty($params['withdraw_id'])) {
            $this->db->where("withdraw.withdraw_id", $params['withdraw_id']);
        }

        if ( ! empty($params['transfer_status'])) {
            $this->db->where_in("withdraw.transfer_status", $params['transfer_status']);
        }

        if ( ! empty($params['startDate'])) {
            $this->db->where("withdraw.created_at >=", $params['startDate']. ' 00:00:01');
        }

        if ( ! empty($params['endDate'])) {
            $this->db->where("withdraw.created_at <=", $params['endDate']. ' 23:59:59');
        }

        $this->db->order_by('transfer_id', 'DESC');
        $this->db->limit($start, $count);
        return $this->db->get();
    }

    /**
     * 统计总数
     */
    public function total($params)
    {
        $this->db->select('*');
        $this->db->from($this->_table);
        $this->db->join($this->_table1, 'transfer_record.transfer_id = withdraw.transfer_id', 'INNER');

        $this->db->where('is_from', 3); //扫码提现
        $this->db->where_in("withdraw.uid", $params['uid']); //提现用户

        if ( ! empty($params['withdraw_id'])) {
            $this->db->where("withdraw.withdraw_id", $params['withdraw_id']);
        }

        if ( ! empty($params['transfer_status'])) {
            $this->db->where_in("withdraw.transfer_status", $params['transfer_status']);
        }

        if ( ! empty($params['startDate'])) {
            $this->db->where("withdraw.created_at >=", $params['startDate']. ' 00:00:01');
        }

        if ( ! empty($params['endDate'])) {
            $this->db->where("withdraw.created_at <=", $params['endDate']. ' 23:59:59');
        }
        return $this->db->count_all_results();
    }

    /**
     * 查找用户详情
     */
    public function findById($transferId, $uid)
    {
        $this->db->select('transfer_record.transfer_id, total_amount, total_num, success_num, success_amount, transfer_record.created_at AS transfer_created_at, transfer_record.updated_at AS transfer_updated_at, withdraw.withdraw_id, withdraw.uid, amount, amount_carry, is_from, withdraw.state, withdraw.transfer_status, bank_id, bank_card, cardholder, bank_name, mobile_phone, audit_status, start_time, pay_time');
        $this->db->from($this->_table);
        $this->db->join($this->_table1, 'transfer_record.transfer_id = withdraw.transfer_id', 'INNER');

        $this->db->where('is_from', 3); //扫码提现
        $this->db->where_in("withdraw.uid", $uid); //提现用户
        $this->db->where_in("transfer_record.transfer_id", $transferId); //转账记录编号
        $this->db->limit(1);
        return $this->db->get();
    }

    /**
     * 结算金额统计
     */
    public function settlementStatistics($params)
    {
        $this->db->select('SUM(withdraw.amount) AS total, transfer_status');
        $this->db->from($this->_table1);
        $this->db->where('is_from', 3); //扫码提现
        $this->db->where("withdraw.uid", $params['uid']); //提现用户
        if ( ! empty($params['startDate'])) {
            $this->db->where("withdraw.created_at >=", $params['startDate']. ' 00:00:01');
        }

        if ( ! empty($params['endDate'])) {
            $this->db->where("withdraw.created_at <=", $params['endDate']. ' 23:59:59');
        }
        $this->db->group_by('transfer_status');
        return $this->db->get();
    }

}